/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.actions.dispositivos;

import com.mapping.Componentes;
import com.mapping.Dispositivo;
import com.mapping.TipoDisp;
import com.opensymphony.xwork2.ActionSupport;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 * @author Andres
 */
@Namespace(value = "/")
@Action(value = "altaComponente", results = {@Result(name = "success", location = "/GestionDispositivos/rAltaComponente.jsp"),
                                              @Result(name = "error",location="/GestionDispositivos/eAltaComponente.jsp")})
public class AltaComponenteAction extends ActionSupport{
    private String idDisp;
    private String idComp;
    private String idTipo;
    private String caracteristicas;
    private String btnEliminar;
    private Componentes comp;
    
    @Override
    public String execute(){
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("inventarios");
        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        tx.begin();
        Dispositivo disp = (Dispositivo)em.getReference(Dispositivo.class, idDisp);
        if(idComp==null){
            comp = new Componentes();
            comp.setIdDispActual(disp);
            comp.setIdDispOrigen(disp);
            comp.setIdTipo((TipoDisp) em.getReference(TipoDisp.class, Integer.parseInt(idTipo)));
            comp.setCaracteristicas(caracteristicas);
            comp.setStatus("Nuevo");
            em.persist(comp);
            em.flush();
        }else{
            comp = em.getReference(Componentes.class, Integer.parseInt(idComp));
            comp.setIdDispActual(disp);
            em.flush();
        }
        tx.commit();
        em.close();
        return SUCCESS;
    }

    public void setIdDisp(String idDisp) {
        this.idDisp = idDisp;
    }

    public void setIdComp(String idComp) {
        this.idComp = idComp;
    }

    public void setIdTipo(String idTipo) {
        this.idTipo = idTipo;
    }

    public void setCaracteristicas(String caracteristicas) {
        this.caracteristicas = caracteristicas;
    }

    public Componentes getComp() {
        return comp;
    }


    public String getBtnEliminar() {
        return btnEliminar;
    }

    public void setBtnEliminar(String btnEliminar) {
        this.btnEliminar = btnEliminar;
    }
}